<?php


declare (strict_types=1);

namespace app\api\controller;

use app\api\service\passport\Login as LoginService;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\response\Json;

/**
 * 用户认证模块
 * Class Passport
 * @package app\api\controller
 */
class Passport extends Controller
{
    /**
     * 登录接口
     * @return array|Json
     * @throws BaseException
     * @throws Exception
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function login()
    {
        // 执行登录
        $LoginService = new LoginService;
        if (!$LoginService->login($this->postForm())) {
            return $this->renderError($LoginService->getError());
        }
        // 用户信息
        $userInfo = $LoginService->getUserInfo();
        return $this->renderSuccess([
            'userId' => (int)$userInfo['student_user_id'],
            'userInfo' => [
                'full_name' => $userInfo['full_name'],
                'id_card' => substr_replace($userInfo['id_card'] ,"**********",4,10),
            ],
            'token' => $LoginService->token
        ], '登录成功');
    }

    /**
     * 修改密码
     * @return array|Json
     * @throws BaseException
     * @throws Exception
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function resetPassword()
    {
        $LoginService = new LoginService;
        if (!$LoginService->resetPassword($this->postForm())) {
            return $this->renderError($LoginService->getError());
        }
        return $this->renderSuccess('修改成功');
    }


}
